package com.chinasofti.java8.demo;
import org.junit.Test;

import java.util.*;

public class MyDemo {
    //1. 说说匿名内部类

    @Test //junit 测试类  返回值类型必须是void  方法名不能是 Test 方法不能有参数
    public void test1(){
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //比较 int 值大小
                return Integer.compare(o1,o2);
            }
        };
        TreeSet<Integer> ts = new TreeSet<>(com);
    }
    @Test
    public void test2(){//lambda
        Comparator<Integer> com = (Integer o1, Integer o2)->Integer.compare(o1,o2);
        TreeSet<Integer> ts = new TreeSet<>(com);
    }

    //创建Employee 类
    /*
    private int id;
    private String name;
    private int age;
    private double salary;
     */
    //模拟数据
    List<Employee> emps = Arrays.asList(
            new Employee(101,"张三",18,9999.99)
            ,new Employee(102,"李四",59,6666.66)
            ,new Employee(103,"王五",28,3333.33)
            ,new Employee(104,"赵六",8,7777.7)
            ,new Employee(105,"田七",38,5555.55)
    );

    //需求 获取公司年龄小于35的员工    filterEmployeeAge
    public List<Employee> filterEmployeeAge(List<Employee> emps){
        List<Employee> list = new ArrayList<>();
        for (Employee emp:emps){
            if(emp.getAge() <= 35){
                list.add(emp);
            }
        }
        return list;
    }
    @Test
    public void test3(){
        List<Employee> employees = filterEmployeeAge(emps);
        System.out.println("employees = " + employees);
    }
    //再来需求 工资大于5000的员工信息
    public List<Employee> filterEmployeeSalary(List<Employee> emps){
        List<Employee> list = new ArrayList<>();
        for (Employee emp:emps){
            if(emp.getSalary() >=5000){
                list.add(emp);
            }
        }
        return list;
    }
    //优化方式一、 策略模式
    /*
    1.设计接口
    package com.chinasofti.java8.demo;

public interface MyPredicate<T> {
    public boolean test(T t);
}
2.实现类
public class FilterEmployeeForAge implements MyPredicate<Employee> {
    @Override
    public boolean test(Employee t) {
        return t.getAge() <= 35;
    }
}
     */
    public List<Employee> filterEmployee(List<Employee> emps,MyPredicate<Employee> mp){
        List<Employee> list = new ArrayList<>();
        for (Employee employee:emps){
            if(mp.test(employee)){
                list.add(employee);
            }
        }
        return list;
    }
    //测试方法
    @Test
    public void test4(){
        List<Employee> list = filterEmployee(emps,new FilterEmployeeForAge());
        System.out.println(list);
        //-------------------------
        List<Employee> list1 = filterEmployee(emps,new FilterEmployeeForSalary());
        System.out.println(list1);
    }
    //优化方式二： 匿名内部类
    @Test
    public void test5(){
        List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee t) {
                return t.getId() <=103;
            }
        });
        System.out.println(list);
    }
    //优化方式三： lambda表达式
    @Test
    public void test6(){
        List<Employee> list = filterEmployee(emps, (emp) ->emp.getId() <=103 );
        System.out.println("list = " + list);
    }
    //优化方式四： stream API
    @Test
    public void test7(){
        emps.stream().filter((e) ->e.getAge() <=35).forEach(System.out::println);
        System.out.println("-----------");
        emps.stream().map(Employee :: getName)
                .limit(3)
                .sorted()
                .forEach(System.out::println);
    }
}